﻿using System;
using System.Collections.Generic;
using System.Web;
using MiWebSite_Core;
using System.Data;
using System.Data.Odbc;
using WLMCoreLibrary;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace MWS_Content
{
    [Serializable]
    public class MiWebSiteContent : BaseClass
    {


        #region public properties
        [XmlAttribute()]
        public int idCategoria { get; set; }
        [XmlAttribute()]
        public string Titulo { get; set; }
        [XmlAttribute()]
        public String Keywords { get; set; }
        [XmlAttribute()]
        public String ContentName { get; set; }
        public String Contenido { get; set; }

        public string ContenidoExtra
        {
            get;
            set;

        }
        public int IdGeoTag
        {
            get;
            set;

        }

        public int IdGaleria
        {
            get;
            set;

        }

        public GeoTag geoTag
        {
            get;
            set;

        }
        public String Portada { get; set; }
        public String Mobile { get; set; }
        public List<MiWebSiteMediaImages> GaleriaImagenes
        {
            get;
            set;
        }

        #endregion

        #region constructors
        public MiWebSiteContent()
        {
        }

        public MiWebSiteContent( ref dbConnector ConexionDB )
        {
            ConexionDBInterna = ConexionDB;
            WLMClassBinder classBinder = new WLMClassBinder( ref ConexionDB );
            id = classBinder.NewRecord( "contenido" );
            string queryString = "";
            queryString = "SELECT * FROM contenido WHERE id=\"" + id + "\"";
            GetRecord( queryString );
        }

        public MiWebSiteContent( int registryId, ref dbConnector ConexionDB )
        {
            ConexionDBInterna = ConexionDB;
            string queryString = "";
            queryString = "SELECT * FROM contenido WHERE id='" + registryId + "'";
            WLMClassBinder classBinder = new WLMClassBinder( ref ConexionDB );

            this.GetRecord( queryString );

        }

        public MiWebSiteContent( string contName, ref dbConnector ConexionDB )
        {
            ConexionDBInterna = ConexionDB;
            string queryString = "";
            queryString = "SELECT * FROM contenido WHERE contentName='" + contName + "'";
            WLMClassBinder classBinder = new WLMClassBinder( ref ConexionDB );

            this.GetRecord( queryString );

        }

        public MiWebSiteContent( Categoria categoria, ref dbConnector ConexionDB )
        {
            ConexionDBInterna = ConexionDB;
            string queryString = "";
            queryString = "SELECT * FROM contenido WHERE idCategoria='" + categoria.id + "' order by id desc limit 1";
            //if (ConexionDBInterna.ExecuteRecordCount(queryString) == 0)
            //    HttpContext.Current.Response.Write("No hay contenido para esto");
            WLMClassBinder classBinder = new WLMClassBinder( ref ConexionDB );
            this.GetRecord( queryString );
        }
        #endregion

        #region public methods

        public void GetAditionalInfo()
        {
            geoTag = new GeoTag( IdGeoTag, ref ConexionDBInterna );
            GaleriaImagenes = new MiWebSiteMediaImagesCollection( IdGaleria, ref ConexionDBInterna ).Elementos;
        }

        public void Delete()
        {
            string queryString = "";
            queryString = "DELETE FROM contenido WHERE id=\"" + id + "\"";
            ConexionDBInterna.ExecuteCommand( queryString );

        }
        #endregion

        #region Private Methods
        private void GetRecord( string queryString )
        {
            OdbcDataReader reader = ConexionDBInterna.ExecuteReader( queryString );
            if ( reader.HasRows )
            {
                if ( !reader.IsDBNull( reader.GetOrdinal( "id" ) ) )
                    id = (System.Int32)reader["id"];
                if ( !reader.IsDBNull( reader.GetOrdinal( "fechaCreacion" ) ) )
                    fechaCreacion = Convert.ToDateTime( reader["fechaCreacion"] );
                if ( !reader.IsDBNull( reader.GetOrdinal( "fechaModificacion" ) ) ) fechaModificacion = Convert.ToDateTime( reader["fechaModificacion"] );
                if ( !reader.IsDBNull( reader.GetOrdinal( "idUsuarioModificacion" ) ) ) idUsuarioModificacion = (System.Int32)reader["idUsuarioModificacion"];

                if ( !reader.IsDBNull( reader.GetOrdinal( "idUsuario" ) ) )
                    idUsuario = (System.Int32)reader["idUsuario"];
                if ( !reader.IsDBNull( reader.GetOrdinal( "idCategoria" ) ) )
                    idCategoria = (System.Int32)reader["idCategoria"];
                if ( !reader.IsDBNull( reader.GetOrdinal( "contentName" ) ) )
                    ContentName = (System.String)reader["contentName"];
                if ( !reader.IsDBNull( reader.GetOrdinal( "contenido" ) ) )
                    Contenido = (System.String)reader["contenido"];
                if ( !reader.IsDBNull( reader.GetOrdinal( "titulo" ) ) )
                    Titulo = (System.String)reader["titulo"];
                if ( !reader.IsDBNull( reader.GetOrdinal( "keywords" ) ) )
                    Keywords = (System.String)reader["keywords"];
                if ( !reader.IsDBNull( reader.GetOrdinal( "contenidoExtra" ) ) )
                    ContenidoExtra = (System.String)reader["contenidoExtra"];
                if ( !reader.IsDBNull( reader.GetOrdinal( "idGeoTag" ) ) )
                    IdGeoTag = (System.Int32)reader["idGeoTag"];
                if ( !reader.IsDBNull( reader.GetOrdinal( "idGaleria" ) ) )
                    IdGaleria = (System.Int32)reader["idGaleria"];
                if ( !reader.IsDBNull( reader.GetOrdinal( "portada" ) ) )
                    Portada = (System.String)reader["portada"];
                if ( !reader.IsDBNull( reader.GetOrdinal( "mobile" ) ) )
                    Mobile = (System.String)reader["mobile"];
            }
        }

        protected override void UpdateBind()
        {
            StringBuilder strCommand = new StringBuilder();
            strCommand.Append( "UPDATE contenido set " ); strCommand.Append( "fechaModificacion='" + this.fechaModificacion.ToString( "yyyy-MM-dd HH:mm:ss" ) + "'" ); strCommand.Append( ",idUsuarioModificacion=\"" + idUsuarioModificacion.ToString() + "\"" );
            strCommand.Append( ",idCategoria=\"" + idCategoria.ToString() + "\"" );
            strCommand.Append( ",titulo=\"" + WLMFunctions.CleanQuery( WLMFunctions.comillas( Titulo ) + "\"" ) );
            strCommand.Append( ",contentName=\"" + WLMFunctions.comillas( ContentName ) + "\"" );
            strCommand.Append( ",contenido=\"" + WLMFunctions.comillas( Contenido.ToString() ) + "\"" );
            strCommand.Append( ",keywords=\"" + Keywords.ToString() + "\"" );
            strCommand.Append( ",contenidoExtra=\"" + WLMFunctions.comillas( ContenidoExtra ) + "\"" );
            strCommand.Append( ",idGeoTag=\"" + IdGeoTag.ToString() + "\"" );
            strCommand.Append( ",idGaleria=\"" + IdGaleria.ToString() + "\"" );
            strCommand.Append( ",portada=\"" + WLMFunctions.comillas( Portada ) + "\"" );
            strCommand.Append( ",mobile=\"" + WLMFunctions.comillas( Mobile ) + "\"" );
            strCommand.Append( " WHERE id=\"" + id + "\"" );
            ConexionDBInterna.ExecuteCommand( strCommand.ToString() );
            //HttpContext.Current.Response.Write(strCommand);
            //HttpContext.Current.Response.End();
        }
        #endregion
    }

    public class Contenido2Dominio : BaseClass
    {


        #region public properties
        public int idContenido { get; set; }
        public int idDominio { get; set; }
        #endregion

        #region constructors
        public Contenido2Dominio( dbConnector dbConn )
        {
            ConexionDBInterna = dbConn;
            WLMClassBinder classBinder = new WLMClassBinder( ref ConexionDBInterna );
            id = classBinder.NewRecord( "CONTENIDO2DOMINIO" );
            string queryString = "";
            queryString = "SELECT * FROM CONTENIDO2DOMINIO WHERE id='" + id + "'";
            GetRecord( queryString );
        }

        public Contenido2Dominio( int registryId, dbConnector dbConn )
        {
            ConexionDBInterna = dbConn;
            string queryString = "";
            queryString = "SELECT * FROM CONTENIDO2DOMINIO WHERE id='" + registryId + "'";
            WLMClassBinder classBinder = new WLMClassBinder( ref ConexionDBInterna );
            this.GetRecord( queryString );
        }
        public Contenido2Dominio( DataRow dataRow, dbConnector dbConn )
        {
            ConexionDBInterna = dbConn;
            this.GetRecord( dataRow );
        }
        #endregion

        #region public methods


        public void Delete()
        {
            string queryString = "";
            queryString = "DELETE FROM CONTENIDO2DOMINIO WHERE id='" + id + "'";
            ConexionDBInterna.ExecuteCommand( queryString );

        }
        #endregion


        #region Private Methods
        private void GetRecord( string queryString )
        {
            OdbcDataReader reader = ConexionDBInterna.ExecuteReader( queryString );
            if ( reader.HasRows )
            {
                if ( !reader.IsDBNull( reader.GetOrdinal( "id" ) ) )
                    id = (System.Int32)reader["id"];
                if ( !reader.IsDBNull( reader.GetOrdinal( "fechaCreacion" ) ) )
                    fechaCreacion = Convert.ToDateTime( reader["fechaCreacion"] );
                if ( !reader.IsDBNull( reader.GetOrdinal( "fechaModificacion" ) ) )
                    fechaModificacion = Convert.ToDateTime( reader["fechaModificacion"] );
                if ( !reader.IsDBNull( reader.GetOrdinal( "idUsuario" ) ) )
                    idUsuario = (System.Int32)reader["idUsuario"];
                if ( !reader.IsDBNull( reader.GetOrdinal( "idUsuarioModificacion" ) ) )
                    idUsuarioModificacion = (System.Int32)reader["idUsuarioModificacion"];
                if ( !reader.IsDBNull( reader.GetOrdinal( "idContenido" ) ) )
                    idContenido = (System.Int32)reader["idContenido"];
                if ( !reader.IsDBNull( reader.GetOrdinal( "idDominio" ) ) )
                    idDominio = (System.Int32)reader["idDominio"];
            }
        }


        private void GetRecord( DataRow dataRow )
        {
            if ( dataRow != null )
            {
                if ( dataRow["id"] != null )
                    id = (System.Int32)dataRow["id"];
                if ( dataRow["fechaCreacion"] != null )
                    fechaCreacion = (System.DateTime)dataRow["fechaCreacion"];
                if ( dataRow["fechaModificacion"] != null )
                    fechaModificacion = (System.DateTime)dataRow["fechaModificacion"];
                if ( dataRow["idUsuario"] != null )
                    idUsuario = (System.Int32)dataRow["idUsuario"];
                if ( dataRow["idUsuarioModificacion"] != null )
                    idUsuarioModificacion = (System.Int32)dataRow["idUsuarioModificacion"];
                if ( dataRow["idContenido"] != null )
                    idContenido = (System.Int32)dataRow["idContenido"];
                if ( dataRow["idDominio"] != null )
                    idDominio = (System.Int32)dataRow["idDominio"];
            }
        }


        protected override void UpdateBind()
        {
            StringBuilder strCommand = new StringBuilder();
            strCommand.Append( "UPDATE CONTENIDO2DOMINIO set " );
            strCommand.Append( "fechaModificacion='" + DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss" ) + "'" );
            strCommand.Append( ",idUsuarioModificacion='" + idUsuarioModificacion.ToString() + "'" );
            strCommand.Append( ",idContenido='" + idContenido.ToString() + "'" );
            strCommand.Append( ",idDominio='" + idDominio.ToString() + "'" );
            strCommand.Append( " WHERE id='" + id + "'" );
            ConexionDBInterna.ExecuteCommand( strCommand.ToString() );
        }
        #endregion
    }

    public class Contenido2DominioCollection
    {
        private dbConnector ConexionDB;
        public BaseClassArray<Contenido2Dominio> Elementos { get; set; }
        public Contenido2DominioCollection()
        {
        }
        public Contenido2DominioCollection( dbConnector dbConn )
        {
            ConexionDB = dbConn;
            string strCommand = "SELECT id FROM CONTENIDO2DOMINIO ";
            OdbcDataReader dataReader = ConexionDB.ExecuteReader( strCommand );
            Elementos = new BaseClassArray<Contenido2Dominio>();
            while ( dataReader.Read() )
            {
                Contenido2Dominio newItem = new Contenido2Dominio( Convert.ToInt32( dataReader["id"] ), ConexionDB );
                Elementos.Add( newItem );
            }
            dataReader.Close();
            dataReader.Dispose();
        }
    }

    public class ContentFunctions
    {
        public static void LoadIncludes( MiWebSitePage pagina )
        {
        }

        public static void LoadIncludesAdmin( MiWebSitePage pagina )
        {
            pagina.Scripts.Add( "/jquery/jquery.scrollTo.js" );
            pagina.Scripts.Add( "/content/js/admin.js" );
            pagina.Scripts.Add( "http://maps.google.com/maps/api/js?sensor=false&amp;language=es" );
            pagina.Scripts.Add( "/jquery/MapsAdmin.js" );
        }
    }

}
